#include "FileMeta.h"
#include "napi/native_api.h"
#include <string>

static napi_value Add(napi_env env, napi_callback_info info)
{
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);

    return sum;

}



static napi_value CreateFileMeta(napi_env env, napi_callback_info info) {
    size_t argc = 4;
    napi_value args[4] = {nullptr}; // 声明参数数组
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);  // 获取传入的参数值并存入参数数组中
    size_t len = 0;
    napi_status status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &len);
    if (status != napi_ok) {
        return nullptr;
    }
    char* name = new char[len + 1];
    std::memset(name, 0, len+1);
    napi_get_value_string_utf8(env, args[0], name, len+1, &len);
    len = 0;
    status = napi_get_value_string_utf8(env, args[1], nullptr, 0, &len);
    if (status != napi_ok) {
        return nullptr;
    }
    char* createDate = new char[len+1];
    std::memset(createDate, 0, len+1);
    napi_get_value_string_utf8(env, args[1], createDate, len+1, &len);
    len = 0;
    status = napi_get_value_string_utf8(env, args[2], nullptr, 0, &len);
    if (status != napi_ok) {
        return nullptr;
    }
    char* author = new char[len+1];
    std::memset(author, 0, len+1);
    napi_get_value_string_utf8(env, args[2], author, len+1, &len);
    int size = 0;
    status = napi_get_value_int32(env, args[3], &size);
    if (status != napi_ok) {
        return nullptr;
    }
    FileMeta* fileMeta = new FileMeta(name, createDate, author, size);
    napi_value result = nullptr;
    napi_create_object(env, &result);
    napi_value property_name = nullptr;
    status = napi_create_string_utf8(env, "file_name", NAPI_AUTO_LENGTH, &property_name);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "创建属性名 name 失败");
        return nullptr;
    }
    napi_value property_value = nullptr;
    status = napi_create_string_utf8(env, fileMeta->getName(), NAPI_AUTO_LENGTH, &property_value);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "创建属性值 name 失败");
        return nullptr;
    }
    status = napi_set_property(env, result, property_name, property_value);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "设置属性 name 失败");
        return nullptr;
    }
    property_name = nullptr;
    status = napi_create_string_utf8(env, "file_create_date", NAPI_AUTO_LENGTH, &property_name);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "创建属性名 create_date 失败");
        return nullptr;
    }
    property_value = nullptr;
    status = napi_create_string_utf8(env, fileMeta->getCreate_Date(), NAPI_AUTO_LENGTH, &property_value);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "创建属性值 create_date 失败");
        return nullptr;
    }
    status = napi_set_property(env, result, property_name, property_value);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "设置属性 create_date 失败");
    }
    property_name = nullptr;
    status = napi_create_string_utf8(env, "file_author", NAPI_AUTO_LENGTH, &property_name);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "创建属性名 author 失败");
        return nullptr;
    }
    property_value = nullptr;
    status = napi_create_string_utf8(env, fileMeta->getAuthor(), NAPI_AUTO_LENGTH, &property_value);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "创建属性值 author 失败");
        return nullptr;
    }
    status = napi_set_property(env, result, property_name, property_value);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "设置属性 author 失败");
        return nullptr;
    }
    property_name = nullptr;
    status = napi_create_string_utf8(env, "file_size", NAPI_AUTO_LENGTH, &property_name);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "创建属性名 size 失败");
        return nullptr;
    }
    property_value = nullptr;
    status = napi_create_int32(env, fileMeta->getSize(), &property_value);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "创建属性值 size 失败");
        return nullptr;
    }
    status = napi_set_property(env, result, property_name, property_value);
    if (status != napi_ok) {
        napi_throw_error(env, "99999", "设置属性 size 失败");
    }
    return result;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"Add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"CreateFileMeta", nullptr, CreateFileMeta, nullptr, nullptr, nullptr, napi_default, nullptr},
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "lib_obj",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterLib_objModule(void)
{
    napi_module_register(&demoModule);
}
